<?php
/**
 * Created by PhpStorm.
 * User: Yan
 * Date: 2017/9/26
 * Time: 14:40
 */

namespace App\Services;


use GuzzleHttp\Client;

class College
{
    private $config = [];
    protected $headers = ['Accept' => 'application/json'];
    private $client;
//    private $getUserInfoUrl = 'http://api.tjise.edu.cn/apiForTjise/api/stuBasicInfo.do?find';
    private $getStudentsUrl = 'http://api.tjise.edu.cn/apiForTjise/api/stuBasicInfo.do?find';
    private $getTeachersUrl = 'http://api.tjise.edu.cn/apiForTjise/api/teacherInfo.do?find';
    private $getOrganizationalUrl = 'http://api.tjise.edu.cn/apiForTjise/api/rjxySystemnode.do?find';

    private $MinzuUrl = 'http://api.tjise.edu.cn/apiForTjise/api/codeMzb.do?find&key=';
    private $MianmaoUrl = 'http://api.tjise.edu.cn/apiForTjise/api/codeZzmmb.do?find&key=';
    private $xuejiUrl = 'http://api.tjise.edu.cn/apiForTjise/api/codeXsb.do?find&key=';

    public function __construct($config)
    {
        $this->config = $config;
        $this->client = new Client();
    }

    public function getOrganizational($level, $page = 0)
    {
        $content = ["level" => $level, 'page' => $page, 'fetchSize' => 100, "key" => $this->config['key']];
        return $this->requestPost($content, $this->getOrganizationalUrl);
    }

    /**
     * @param $u_code
     * @return bool
     */
    public function user($u_code)
    {
        $content = ["xh" => $u_code, "key" => $this->config['key']];
        return $this->requestPost($content, $this->getStudentsUrl);
    }

    /**
     * @param $jxb
     * @param $page
     * @return bool
     */
    public function StudentsJx($jxb, $page)
    {
        $content = ['jxb' => $jxb, "page" => $page, 'fetchSize' => 100, "key" => $this->config['key']];
        $jx = $this->requestPost($content, $this->getStudentsUrl);
        return $jx;
    }

    public function StudentsYw($jxb, $page)
    {
        $content = ['ywb' => $jxb, "page" => $page, 'fetchSize' => 100, "key" => $this->config['key']];
        $yw = $this->requestPost($content, $this->getStudentsUrl);
        return $yw;
    }

    public function Teachers($bm, $page)
    {
        $content = ['bm' => $bm, "page" => $page, 'fetchSize' => 100, "key" => $this->config['key']];
        return $this->requestPost($content, $this->getTeachersUrl);
    }

    public function getNations()
    {
        return $this->requestGet($this->MinzuUrl . $this->config['list_key'].'&fetchSize=100');
    }

    public function getMianMao()
    {
        return $this->requestGet($this->MianmaoUrl . $this->config['list_key'].'&fetchSize=100');
    }

    public function geXueji()
    {
        return $this->requestGet($this->xuejiUrl . $this->config['list_key'].'&fetchSize=100');
    }

    public function requestPost($content, $url)
    {
        $response = $this->client->request('POST', $url, [
            'form_params' => $content,
            'headers' => $this->headers
        ]);
        $result = json_decode($response->getBody());
        if (empty($result) || $result->success == false || empty($result->attr->result)) {
            return false;
        }
        return $result->attr->result;
    }

    public function requestGet($url)
    {
        $response = $this->client->request('GET', $url);
        $result = json_decode($response->getBody());
        if (empty($result) || $result->success == false || empty($result->attr->result)) {
            return false;
        }
        return $result->attr->result;
    }
}